package com.byd.android.agreement.order;

import android.util.Log;

import com.byd.android.agreement.BaseMessage;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Params extends BaseMessage {

    public class Param {
        public String name;
        public String value;
    }

    public static final String PARAMS = "params";

    public List<Param> mParams;

    public Params() {
        mParams = new ArrayList<Param>();
    }

    public void addParam(Param param) {
        mParams.add(param);
    }

    public void addParam(String name, Object value) {
        Param param = new Param();
        param.name = name;
        param.value = value.toString();
        mParams.add(param);
    }

    public List<Param> getParamList() {
        return mParams;
    }

    public void removeParam(String name) {
        if (name == null) {
            return;
        }
        for (Param p : mParams) {
            if (name.equals(p.name)) {
                mParams.remove(p);
            }
        }
    }

    public String getParamValue(String name) {
        if (name == null) {
            return null;
        }
        for (Param p : mParams) {
            if (name.equals(p.name)) {
                return p.value;
            }
        }
        return null;
    }

    public static String packToJson(Params params) {
        JsonObject jobj = new JsonObject();
        jobj.addProperty(PARAMS, mGs.toJson(params.getParamList()));
        return jobj.toString();
    }

    public static Params parseToJson(String jstr) {
        Params params = new Params();
        JsonObject jobj =  mParser.parse(jstr).getAsJsonObject();
        String ls = jobj.get(PARAMS).getAsString();
        params.mParams = mGs.fromJson(ls, new TypeToken<List<Param>>(){}.getType());

        return params;
    }

}

